﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Configuration;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using LeadThing.AbpZeroTemplate.WorkFlow_Run_Processs.Authorization;
using LeadThing.AbpZeroTemplate.WorkFlow_Run_Processs.Dtos;
using LeadThing.AbpZeroTemplate.Dto;

namespace LeadThing.AbpZeroTemplate.WorkFlow_Run_Processs
{
    /// <summary>
    /// 实例节点流转明细服务实现
    /// </summary>
    //[AbpAuthorize(WorkFlow_Run_ProcessAppPermissions.WorkFlow_Run_Process)]


    public class WorkFlow_Run_ProcessAppService : AbpZeroTemplateAppServiceBase, IWorkFlow_Run_ProcessAppService
    {
        private readonly IRepository<WorkFlow_Run_Process, long> _workFlow_Run_ProcessRepository;
        private readonly IWorkFlow_Run_ProcessListExcelExporter _workFlow_Run_ProcessListExcelExporter;


        private readonly WorkFlow_Run_ProcessManage _workFlow_Run_ProcessManage;
        /// <summary>
        /// 构造方法
        /// </summary>
        public WorkFlow_Run_ProcessAppService(IRepository<WorkFlow_Run_Process, long> workFlow_Run_ProcessRepository,
WorkFlow_Run_ProcessManage workFlow_Run_ProcessManage
      , IWorkFlow_Run_ProcessListExcelExporter workFlow_Run_ProcessListExcelExporter
  )
        {
            _workFlow_Run_ProcessRepository = workFlow_Run_ProcessRepository;
            _workFlow_Run_ProcessManage = workFlow_Run_ProcessManage;
            _workFlow_Run_ProcessListExcelExporter = workFlow_Run_ProcessListExcelExporter;
        }


        #region 实体的自定义扩展方法
        private IQueryable<WorkFlow_Run_Process> _workFlow_Run_ProcessRepositoryAsNoTrack => _workFlow_Run_ProcessRepository.GetAll().AsNoTracking();


        #endregion


        #region 实例节点流转明细管理

        /// <summary>
        /// 根据查询条件获取实例节点流转明细分页列表
        /// </summary>
        public async Task<PagedResultDto<WorkFlow_Run_ProcessListDto>> GetPagedWorkFlow_Run_ProcesssAsync(GetWorkFlow_Run_ProcessInput input)
        {

            var query = _workFlow_Run_ProcessRepositoryAsNoTrack;
            //TODO:根据传入的参数添加过滤条件

            var workFlow_Run_ProcessCount = await query.CountAsync();

            var workFlow_Run_Processs = await query
            .OrderBy(input.Sorting)
            .PageBy(input)
            .ToListAsync();

            var workFlow_Run_ProcessListDtos = workFlow_Run_Processs.MapTo<List<WorkFlow_Run_ProcessListDto>>();
            return new PagedResultDto<WorkFlow_Run_ProcessListDto>(
            workFlow_Run_ProcessCount,
            workFlow_Run_ProcessListDtos
            );
        }

        /// <summary>
        /// 通过Id获取实例节点流转明细信息进行编辑或修改 
        /// </summary>
        public async Task<GetWorkFlow_Run_ProcessForEditOutput> GetWorkFlow_Run_ProcessForEditAsync(NullableIdDto<long> input)
        {
            var output = new GetWorkFlow_Run_ProcessForEditOutput();

            WorkFlow_Run_ProcessEditDto workFlow_Run_ProcessEditDto;

            if (input.Id.HasValue)
            {
                var entity = await _workFlow_Run_ProcessRepository.GetAsync(input.Id.Value);
                workFlow_Run_ProcessEditDto = entity.MapTo<WorkFlow_Run_ProcessEditDto>();
            }
            else
            {
                workFlow_Run_ProcessEditDto = new WorkFlow_Run_ProcessEditDto();
            }

            output.WorkFlow_Run_Process = workFlow_Run_ProcessEditDto;
            return output;
        }


        /// <summary>
        /// 通过指定id获取实例节点流转明细ListDto信息
        /// </summary>
        public async Task<WorkFlow_Run_ProcessListDto> GetWorkFlow_Run_ProcessByIdAsync(EntityDto<long> input)
        {
            var entity = await _workFlow_Run_ProcessRepository.GetAsync(input.Id);

            return entity.MapTo<WorkFlow_Run_ProcessListDto>();
        }







        /// <summary>
        /// 新增或更改实例节点流转明细
        /// </summary>
        public async Task CreateOrUpdateWorkFlow_Run_ProcessAsync(CreateOrUpdateWorkFlow_Run_ProcessInput input)
        {
            if (input.WorkFlow_Run_ProcessEditDto.Id.HasValue)
            {
                await UpdateWorkFlow_Run_ProcessAsync(input.WorkFlow_Run_ProcessEditDto);
            }
            else
            {
                await CreateWorkFlow_Run_ProcessAsync(input.WorkFlow_Run_ProcessEditDto);
            }
        }

        /// <summary>
        /// 新增实例节点流转明细
        /// </summary>
        //[AbpAuthorize(WorkFlow_Run_ProcessAppPermissions.WorkFlow_Run_Process_CreateWorkFlow_Run_Process)]
        public virtual async Task<WorkFlow_Run_ProcessEditDto> CreateWorkFlow_Run_ProcessAsync(WorkFlow_Run_ProcessEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = input.MapTo<WorkFlow_Run_Process>();

            entity = await _workFlow_Run_ProcessRepository.InsertAsync(entity);
            return entity.MapTo<WorkFlow_Run_ProcessEditDto>();
        }

        /// <summary>
        /// 编辑实例节点流转明细
        /// </summary>
        //[AbpAuthorize(WorkFlow_Run_ProcessAppPermissions.WorkFlow_Run_Process_EditWorkFlow_Run_Process)]
        public virtual async Task UpdateWorkFlow_Run_ProcessAsync(WorkFlow_Run_ProcessEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新

            var entity = await _workFlow_Run_ProcessRepository.GetAsync(input.Id.Value);
            input.MapTo(entity);

            await _workFlow_Run_ProcessRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 删除实例节点流转明细
        /// </summary>
        //[AbpAuthorize(WorkFlow_Run_ProcessAppPermissions.WorkFlow_Run_Process_DeleteWorkFlow_Run_Process)]
        public async Task DeleteWorkFlow_Run_ProcessAsync(EntityDto<long> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _workFlow_Run_ProcessRepository.DeleteAsync(input.Id);
        }

        /// <summary>
        /// 批量删除实例节点流转明细
        /// </summary>
        //[AbpAuthorize(WorkFlow_Run_ProcessAppPermissions.WorkFlow_Run_Process_DeleteWorkFlow_Run_Process)]
        public async Task BatchDeleteWorkFlow_Run_ProcessAsync(List<long> input)
        {
            //TODO:批量删除前的逻辑判断，是否允许删除
            await _workFlow_Run_ProcessRepository.DeleteAsync(s => input.Contains(s.Id));
        }

        #endregion

        #region 实例节点流转明细的Excel导出功能


        public async Task<FileDto> GetWorkFlow_Run_ProcessToExcel()
        {
            var entities = await _workFlow_Run_ProcessRepository.GetAll().ToListAsync();

            var dtos = entities.MapTo<List<WorkFlow_Run_ProcessListDto>>();

            var fileDto = _workFlow_Run_ProcessListExcelExporter.ExportWorkFlow_Run_ProcessToFile(dtos);



            return fileDto;
        }


        #endregion

    }
}
